package com.echo.fm.platform.auth;

import com.echo.fm.content.systemAction.model.SystemAction;
import com.echo.fm.platform.auth.model.Authority;
import com.echo.fm.platform.auth.model.LoginPass;
import com.echo.fm.platform.auth.model.RoleAuth;
import com.echo.fm.platform.auth.model.SystemUser;
import com.echo.fm.user.role.model.Role;
import com.echo.util.ErrorException;
import com.echo.util.TimeGetter;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.util.List;
import java.util.UUID;

@Service("authService")
public class DefaultAuthServiceImpl implements IAuthService {
	@Autowired
	@Qualifier("authPersistence")
	protected IAuthPersistence authPersistence;

	public SystemUser searchSystemUserByUsername(String userName) throws ErrorException {

		return authPersistence.searchSystemUserByUsername(userName);
	}

	public boolean checkPassword(String uid, String password) throws ErrorException {

		return authPersistence.checkPassword(uid, password);
	}

	public void registSystemUser(String userName, String password, String phone) throws ErrorException {
		SystemUser systemUser = new SystemUser();
//		systemUser.setPhone(phone);
//		systemUser.setPassword(password);
//		systemUser.setUsername(userName);
//		systemUser.setT(TimeGetter.getTime());
//		systemUser.setSystemuid(UUID.randomUUID().toString().replaceAll("-", ""));
		authPersistence.registSystemUser(systemUser);
	}

	public boolean authority(String token, String action) {
		if (token == null) {
			return false;
		}
		if ("".equals(action)) {
			return true;
		}
		return true;
	}

	public String login(String username, byte[] md5) throws ErrorException {

		String userId = searchUserIdByUsername(username);
		if (userId != null) {
			LoginPass loginPass = authPersistence.searchLoginPassByUId(userId);
			Boolean isPassSuccess = MessageDigest.isEqual(loginPass.getPassword(), md5);
			if (isPassSuccess) {
				String uuidToken = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
				return uuidToken;
			}
		}
		return null;
	}

	public Authority searchAuthorityByUId(String uid) throws ErrorException {
		return authPersistence.searchAuthorityByUId(uid);
	}

	public Authority searchAuthorityByToken(String token) throws ErrorException {
		return authPersistence.searchAuthorityByToken(token);
	}

	public List<Authority> searchAuthorityList(boolean isTest) throws ErrorException {
		return authPersistence.searchAuthorityList(isTest);
	}

	public List<Authority> searchAuthorityList() throws ErrorException {
		return authPersistence.searchAuthorityList();
	}

	public void updateAuthority(Authority authority) throws ErrorException {
		authPersistence.updateAuthority(authority);

	}

	public String searchUserIdByUsername(String username) throws ErrorException {
		return authPersistence.searchUserIdByUsername(username);
	}

	public LoginPass searchLoginPassByUId(String userId) throws ErrorException {
		return authPersistence.searchLoginPassByUId(userId);
	}

	public List<RoleAuth> listRoleAuth(Integer roleId) throws ErrorException {
		return authPersistence.listRoleAuth(roleId);
	}

	public List<Role> searchRoleList(List<Criterion> criterions) throws ErrorException {
		return authPersistence.searchRoleList(criterions);
	}

	public List<Role> searcheRoleList(SystemUser user) throws ErrorException {
		return authPersistence.searchRoleList(user);
	}

	public boolean checkLogin(String id, String password) throws ErrorException {
		return authPersistence.checkLogin(id, password);
	}

	public List<SystemAction> getAllSystemActions() throws ErrorException {
		return authPersistence.getAllSystemActions();
	}

	public boolean checkAppLogin(String uid, String password) throws ErrorException {
		return authPersistence.checkAppuser(uid, password);
	}

	  
}
